package com.example.androidproject.novel.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.androidproject.R;
import com.example.androidproject.novel.bean.ContentBean;
import com.example.androidproject.novel.bean.NovelBean;
import com.example.androidproject.retrofit.RetrofitService;
import com.example.androidproject.utils.GlobalFunction;
import com.example.androidproject.utils.GlobalVariable;
import com.example.androidproject.utils.RequestConstant;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NovelContentActivity extends Activity implements View.OnClickListener {
    private final String TAG = "GGSTUDY";
    private NovelBean.DataBean data;
    private Retrofit retrofit;

    private RelativeLayout ll_novel_content;
    private TextView tv_novel_content;
    private String catatitle;
    private ScrollView sv_novel_content;
    private LinearLayout ll_fun_box;
    private Button btn_changeSmall, btn_changeNormal, btn_changeBig, btn_TurnOff, btn_TurnOn;
    private float TextNormalPx;
    private float NormalSp;
    private int btnFlag = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_novel_content);
        //设置顶部状态栏为透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        retrofit = new Retrofit.Builder().baseUrl(RequestConstant.BASE).addConverterFactory(GsonConverterFactory.create()).build();
        data = getIntent().getParcelableExtra("data");
        catatitle = getIntent().getStringExtra("catatitle");
        initView();
        initPage();
        getContent();
    }

    private void initView() {
        ll_novel_content = findViewById(R.id.ll_novel_content);
        tv_novel_content = findViewById(R.id.tv_novel_content);
        sv_novel_content = findViewById(R.id.sv_novel_content);
        ll_fun_box = findViewById(R.id.ll_fun_box);
        btn_changeSmall = findViewById(R.id.btn_changeSmall);
        btn_changeNormal = findViewById(R.id.btn_changeNormal);
        btn_changeBig = findViewById(R.id.btn_changeBig);
        btn_TurnOn = findViewById(R.id.btn_TurnOn);
        btn_TurnOff = findViewById(R.id.btn_TurnOff);
    }

    private void initPage() {
        GlobalFunction.hideStatusBar(this);
        setMarginTop();
        //
        TextNormalPx = tv_novel_content.getTextSize();
        NormalSp = TextNormalPx / GlobalVariable.scaledDensity;
        // 这是点击监听
        btn_changeSmall.setOnClickListener(this);
        btn_changeNormal.setOnClickListener(this);
        btn_changeBig.setOnClickListener(this);
        btn_TurnOff.setOnClickListener(this);
        btn_TurnOn.setOnClickListener(this);
    }

    void setMarginTop() {
        ViewTreeObserver observer = ll_fun_box.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ll_fun_box.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int width = ll_fun_box.getWidth();
                int height = ll_fun_box.getMeasuredHeight();
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins((int) (10 * GlobalVariable.scaledDensity), height, 0, 0);
                sv_novel_content.setLayoutParams(layoutParams);
            }
        });
    }

    private void getContent() {
        String url = RequestConstant.NOVEL_CONTENT_URL + "?novelname=" + data.getTitle() + "&classify=" + data.getClassify() + "&catatitle=" + catatitle;
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<ContentBean> call = retrofitService.getContent(url);
        call.enqueue(new Callback<ContentBean>() {
            @Override
            public void onResponse(Call<ContentBean> call, Response<ContentBean> response) {
                if (response.isSuccessful()) {
                    ContentBean body = response.body();
                    assert body != null;
                    tv_novel_content.setText(body.getData());

                } else {
                    Toast.makeText(NovelContentActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ContentBean> call, Throwable t) {

            }
        });
    }

    public void compareFlag(int temp, int current) {
        if (temp != current) {
            btnFlag = current;
        }
    }

    @SuppressLint({"NonConstantResourceId"})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_TurnOn:
                btn_TurnOff.setVisibility(View.VISIBLE);
                btn_TurnOn.setVisibility(View.GONE);
                ll_novel_content.setBackgroundResource(R.color.novelLight);
                tv_novel_content.setTextColor(Color.parseColor("#000000"));
                break;
            case R.id.btn_TurnOff:
                btn_TurnOn.setVisibility(View.VISIBLE);
                btn_TurnOff.setVisibility(View.GONE);
                ll_novel_content.setBackgroundResource(R.color.black);
                tv_novel_content.setTextColor(Color.parseColor("#ffffff"));
                break;
            case R.id.btn_changeBig:
                tv_novel_content.setTextSize(20);
                break;
            case R.id.btn_changeNormal:
                tv_novel_content.setTextSize(NormalSp);
                break;
            case R.id.btn_changeSmall:
                tv_novel_content.setTextSize(12);
                break;
        }
    }
}